const fileCache = require('think-cache-file');
const nunjucks = require('think-view-nunjucks');
const fileSession = require('think-session-file');
const mysql = require('think-model-mysql');
const path = require('path');
const {think} = require('thinkjs');
const {Console, File, DateFile} = require('think-logger3');
const isDev = think.env === 'development';
const socketio = require('think-websocket-socket.io');
/**
 * cache adapter config
 * @type {Object}
 */
exports.cache = {
    type: 'file',
    common: {
        timeout: 24 * 60 * 60 * 1000 // millisecond
    },
    file: {
        handle: fileCache,
        cachePath: path.join(think.ROOT_PATH, 'runtime/cache'), // absoulte path is necessarily required
        pathDepth: 1,
        gcInterval: 24 * 60 * 60 * 1000 // gc interval
    }
};

/**
 * model adapter config
 * @type {Object}
 */
exports.model = {
    type: 'mysql',
    common: {
        logConnect: true,
        logSql: true,
        logger: (msg: string) => think.logger.debug(msg)
    },
    mysql: {
        handle: mysql,
        database: 'big_pineapple_poker',
        prefix: '',
        encoding: 'utf8mb4',
        charset: 'utf8mb4',
        host: 'rm-wz9lo62446198hi8e7o.mysql.rds.aliyuncs.com',
        port: '3306',
        user: 'root_rds',
        password: 'Demo1234',
        dateStrings: true
    },
    mysql2: {
        handle: mysql,
        database: 'texas_holdem',
        prefix: '',
        encoding: 'utf8mb4',
        charset: 'utf8mb4',
        host: 'rm-wz9lo62446198hi8e7o.mysql.rds.aliyuncs.com',
        port: '3306',
        user: 'root_rds',
        password: 'Demo1234',
        dateStrings: true
    }
};

/**
 * session adapter config
 * @type {Object}
 */
exports.session = {
    type: 'file',
    common: {
        cookie: {
            name: 'thinkjs'
            // keys: ['werwer', 'werwer'],
            // signed: true
        }
    },
    file: {
        handle: fileSession,
        sessionPath: path.join(think.ROOT_PATH, 'runtime/session')
    }
};

/**
 * view adapter config
 * @type {Object}
 */
exports.view = {
    type: 'nunjucks',
    common: {
        viewPath: path.join(think.ROOT_PATH, 'view'),
        sep: '_',
        extname: '.html'
    },
    nunjucks: {
        handle: nunjucks
    }
};
exports.websocket = {
    type: 'socketio',
    common: {
        // common config
    },
    socketio: {
        handle: socketio,
        on: true,
        allowOrigin: null,
        path: '/socket.io',             // 默认 '/socket.io'
        adapter: null,                  // 默认无 adapter
        messages: {
            open: '/websocket/index/open',
            joined: '/websocket/index/open',
            logout: '/api/game/getCurrentGameList/post',
            updateLoginInfo: '/websocket/index/updateLoginInfo',
            uploadVerifyImg: '/websocket/index/uploadVerifyImg',
        }
    }
};


/**
 * logger adapter config
 * @type {Object}
 */
exports.logger = {
    type: isDev ? 'console' : 'dateFile',
    console: {
        handle: Console
    },
    dateFile: {
        handle: DateFile,
        level: 'ALL',
        absolute: true,
        pattern: '-yyyy-MM-dd',
        alwaysIncludePattern: true,
        filename: path.join(think.ROOT_PATH, 'logs/app.log')
    }
};
